import json

# 1.字符串   和   dict list 转换
#  1.1 字符串  -->  dict  list
data = '[{"name":"张三","age":20},{"name":"李四","age":21},{"name":"王五","age":22}]'
list_data = json.loads(data)
print(list_data[1].get("age"))
#  1.2 dict list   --->  字符串
list = [{"name":"张三","age":20},{"name":"李四","age":21},{"name":"王五","age":22}]
json_list_str = json.dumps(list)
print(type(json_list_str))


# 2.文件对象  和 dict  list 转换
#   2.1 dict list  写入文件
list_1 = [{"name":"张三","age":20},{"name":"李四","age":21},{"name":"王五","age":22}]
json.dump(list_1, open('test_json.jon', 'w'))

# with open('test_json.json', 'w') as f:
#     f.write(list)

#   2.2 读取文件   json  --- >> list dict
fp = open('test_json.jon', 'r')
result_list = json.load(fp)
print(result_list[1])







